package com.xc.disk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.disk.entity.UserEntity;
import com.xc.disk.exception.OperateException;
import com.xc.disk.mapper.UserMapper;
import com.xc.disk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>项目名称: Stars</p>
 * <p>描述: 用户接口实现类</p>
 * <P>创建人:小程 </p>
 * <P>创建人邮箱: 779943476@qq.com</p>
 * <p>创建时间: 2019/10/23</p>
 *
 * @version v1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserEntity getUserByAppId(String appId) {
        UserEntity userEntity = new UserEntity();
        userEntity.setAppId(appId);
        return userMapper.selectOne(new QueryWrapper<>(userEntity));
    }

    @Override
    public UserEntity getUserById(String id) {
        return userMapper.selectById(id);
    }

    @Override
    public UserEntity updateUser(UserEntity userEntity) {
        if (!this.updateById(userEntity)) {
            throw new OperateException("c002", "修改失败！");
        }
        return userEntity;
    }

    @Override
    public void computeFreeFlow(String userId, long size, boolean isAdd) {
        //验证可用空间是否充足
        synchronized ("") {
            UserEntity user = userMapper.selectById(userId);
            //计算可用空间
            if (isAdd) {
                long freeFlow = user.getFreeFlow() + size;
                user.setFreeFlow(freeFlow);
            } else {
                if (user.getFreeFlow() < size) {
                    throw new OperateException("f033", "你的积分不足！");
                }
                long freeFlow = user.getFreeFlow() - size;
                user.setFreeFlow(freeFlow);
            }
            if (!retBool(userMapper.updateById(user))) {
                throw new OperateException("f052", "修改用户信息失败！");
            }
        }
    }

    @Override
    public void computeUseSpace(String userId, long size, boolean isAdd) {
        //验证用户可用空间是否可以存下当前文件
        synchronized ("") {
            UserEntity user = userMapper.selectById(userId);
            if (isAdd) {
                if ((user.getCloudSpace() - user.getUseSpace()) < size) {
                    throw new OperateException("f033", "你的云盘空间不足！");
                }
                //修改使用空间
                user.setUseSpace(user.getUseSpace() + size);
            } else {
                user.setUseSpace(user.getUseSpace() - size);
            }
            if (!retBool(userMapper.updateById(user))) {
                throw new OperateException("f052", "修改用户信息失败！");
            }
        }
    }

    @Override
    public UserEntity getUserByShareCode(String shareCode) {
        UserEntity userEntity = new UserEntity();
        userEntity.setShareCode(shareCode);
        return this.getOne(new QueryWrapper<>(userEntity));
    }
}
